package cmd

import (
	"log"
	"time"

	"BookStack/internal/domain/navigation"

	"gorm.io/gorm"
)

// CreateSampleNavigations 创建示例导航数据
func CreateSampleNavigations(db *gorm.DB) error {
	log.Println("开始创建导航示例数据...")

	// 检查是否已存在导航数据
	var count int64
	if err := db.Table("navigations").Count(&count).Error; err != nil {
		return err
	}

	if count > 0 {
		log.Println("导航示例数据已存在，跳过创建")
		return nil
	}

	// 创建头部导航
	headerNav := []struct {
		Name        string
		URL         string
		Icon        string
		Position    string
		ParentID    *uint
		Order       int
		Status      int
		Target      string
		Description string
		CreatedAt   time.Time
		UpdatedAt   time.Time
	}{
		{
			Name:        "首页",
			URL:         "/",
			Icon:        "home",
			Position:    navigation.PositionHeader,
			ParentID:    nil,
			Order:       1,
			Description: "网站首页",
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "文章",
			URL:         "/articles",
			Icon:        "article",
			Position:    navigation.PositionHeader,
			ParentID:    nil,
			Order:       2,
			Description: "文章列表页",
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "书籍",
			URL:         "/books",
			Icon:        "book",
			Position:    navigation.PositionHeader,
			ParentID:    nil,
			Order:       3,
			Description: "书籍列表页",
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
	}

	// 插入头部导航数据
	if err := db.Table("navigations").Create(&headerNav).Error; err != nil {
		return err
	}

	// 创建底部导航
	footerNav := []struct {
		Name        string
		URL         string
		Icon        string
		Position    string
		ParentID    *uint
		Order       int
		Status      int
		Target      string
		Description string
		CreatedAt   time.Time
		UpdatedAt   time.Time
	}{
		{
			Name:        "关于我们",
			URL:         "/about",
			Icon:        "",
			Position:    navigation.PositionFooter,
			ParentID:    nil,
			Order:       1,
			Description: "关于我们页面",
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name: "联系我们",
			URL:  "/contact",
			Icon: "",
		},
		{
			Name:        "隐私政策",
			URL:         "/privacy",
			Icon:        "",
			Position:    navigation.PositionFooter,
			ParentID:    nil,
			Order:       3,
			Description: "隐私政策页面",
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
	}

	// 插入底部导航数据
	if err := db.Table("navigations").Create(&footerNav).Error; err != nil {
		return err
	}

	// 创建移动端导航
	mobileNav := []struct {
		Name        string
		URL         string
		Icon        string
		Position    string
		ParentID    *uint
		Order       int
		Status      int
		Target      string
		Description string
		CreatedAt   time.Time
		UpdatedAt   time.Time
	}{
		{
			Name:        "首页",
			URL:         "/",
			Icon:        "home",
			Position:    navigation.PositionMobile,
			ParentID:    nil,
			Order:       1,
			Description: "网站首页",
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "分类",
			URL:         "/categories",
			Icon:        "category",
			Position:    navigation.PositionMobile,
			ParentID:    nil,
			Order:       2,
			Description: "分类页面",
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "用户",
			URL:         "/user",
			Icon:        "user",
			Position:    navigation.PositionMobile,
			ParentID:    nil,
			Order:       3,
			Description: "用户中心",
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
	}

	// 插入移动端导航数据
	if err := db.Table("navigations").Create(&mobileNav).Error; err != nil {
		return err
	}

	log.Println("导航示例数据创建完成")
	return nil
}
